
$(function () {
    $("#jqGrid").jqGrid({
        url: '../qygj/country/list',
        datatype: "json",
        colModel: [			
			{ label: '洲', name: 'contientName', width: 80},
			{ label: '名称', name: 'name', width: 80 },
			{ label: '英文名', name: 'enName', width: 80 },
			{ label: '首字母', name: 'firstCharacter', width: 80 },
			{ label: '图标', name: 'iconName', width: 80 , formatter : function(cellValue, options, rowObject) {
				if(cellValue) {
					return '<a href="javascript:vm.show(\'' + rowObject.icon + '\');">' + cellValue + '</a>';
				} else {
					return '';
				}
			}},
			{ label: '图片', name: 'imgName', width: 80 , formatter : function(cellValue, options, rowObject) {
				if(cellValue) {
					return '<a href="javascript:vm.show(\'' + rowObject.img + '\');">' + cellValue + '</a>';
				} else {
					return '';
				}
			}},
			{ label: '通过率', name: 'passRate', width: 80 },
			{ label: '办理件数', name: 'transactNum', width: 80 },
			{ label: 'icon', name: 'icon', width: 80 , hidden:true},
			{ label: 'img', name: 'img', width: 80 , hidden:true}
			//{ label: '国旗', name: 'countryFlagName', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.loadContient();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		baseUrl : '../f/d/',
		q:{
			key: null,
			flagkey : null,
			filekey : null
		},
		showList: true,
		title: null,
		country: {
			countryFlagName : null,
			iconName : null,
			imgName : null
		},
		contients : [],
		nationalFlagList : null,
		url : null
	},
	methods: {
		//加载洲数据
		loadContient : function() {
			$.ajax({
				type: "POST",
			    url: "../qygj/country/queryAllContient",
                contentType: "application/json",
			    success: function(r){
					if(r.code == 0){
						vm.contients = r.contients;
					}
				}
			});
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "添加国家";
			vm.country = {countryFlagName : null, iconName : null, imgName : null};
			//$('#countryFlagName').val('');
			var url = vm.country.id == null ? "../qygj/country/save" : "../qygj/country/update";
			$('#form').attr("action", url);
		},
		update: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			
			$.get("../qygj/country/info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改国家信息";
                vm.country = r.country;
                var url = vm.country.id == null ? "../qygj/country/save" : "../qygj/country/update";
    			$('#form').attr("action", url);
            });
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../qygj/country/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function (event) {
			var url = vm.country.id == null ? "../qygj/country/save" : "../qygj/country/update";
			console.dir(vm.country);
			$.ajax({
				type: "POST",
			    url: url,
			    dataType : 'json',
                contentType: "application/json",
			    data: JSON.stringify(vm.country),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'key': vm.q.key},
                page:page
            }).trigger("reloadGrid");
		},
		showFlag : function(event) {
			$('.select-flag').removeClass('select-flag');
			if(!vm.nationalFlagList) {
				$.get("../nationalflag/query", function(r){
					vm.nationalFlagList = r.nationalFlag;
	            });
			}
			layer.open({
				type:1,
				title: "选择国旗",
				area: ['600px', '470px'],
				shadeClose: false,
				shade: 0.000001,
				anim : 4,
				resize : true,
				content: $("#showFlag"),
				btn: ['确认', '取消'],
				btn1: function (index) {
					var len = $('.select-flag').length;
					
					if($('.select-flag').length == 0) {
						alert('请选择一张国旗');
						return;
					}
					var countryName = $('.select-flag').find('.country-name').text();
					var shortUrl = $('.select-flag').find('.country-name').attr('data');
					vm.country.countryFlagName = countryName;
					vm.country.nationalFlag = shortUrl;
					layer.close(index);
	            },
	            btn2: function (index) {
					layer.close(index);
	            }
			});
		},
		selectFlag : function(event) {
			var dom = event.currentTarget
			
			$('.select-flag').removeClass('select-flag');
			$(dom).addClass('select-flag');
		},
		queryFlag : function(event) {
			var countryName = vm.q.flagkey
			$.get("../nationalflag/query", {'flagkey' : countryName}, function(r){
				vm.nationalFlagList = r.nationalFlag;
            });
		},
		showIcon : function(event) {
			showFile(true, vm.setIcon);
		},
		setIcon : function(name, url) {
			vm.country.iconName = name;
		    vm.country.icon = url;
		},
		showImg : function(event) {
			showFile(true, vm.setImg);
		},
		setImg : function(name, url) {
			vm.country.imgName = name;
		    vm.country.img = url;
		},
		show : function(url) {
			vm.url = '../f/d/' + url;
			layer.open({
				type:1,
				title: "查看图片",
				area: ['550px', '470px'],
				shadeClose: false,
				shade: 0.000001,
				anim : 4,
				resize : true,
				content: $("#show"),
				btn: ['确认'],
				btn1: function (index) {
					layer.close(index);
	            }
			});
		}
	}
});


























